package com.kinyx.framework.db.sentence.str;

import java.util.Collections;
import java.util.List;

import com.kinyx.framework.annotation.assist.Tester;
import com.kinyx.framework.bean.StringFormatter;
import com.kinyx.framework.db.sentence.defines.DeleteSentence;
import com.kinyx.framework.enums.impl.ErrorCodeX;
import com.kinyx.framework.exception.KRuntimeException;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

/**
 * 删除语句
 */
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@Tester("com.kinyx.framework.db.sentence.str.demo.delete.DELETE__Tester")
public class DELETE extends _CRUD implements DeleteSentence {

	/** 表子句 */
	private final String table;

	// ================================================================= WHERE ================================================================= //
	/** WHERE子句 */
	private SQL whereCodition;

	/**
	 * 追加WHERE子句
	 * @param condition 条件子句 
	 * @return 本对象
	 */
	public DELETE where(final SQL condition) {
		if (this.whereCodition != null) { throw new KRuntimeException(ErrorCodeX.RepeatCall); }
		this.whereCodition = condition;
		return this;
	}

	// ================================================================= 实现方法 ================================================================= //
	@Override
	public String getSentence() {
		return StringFormatter.of()//
				.x("DELETE FROM %s", this.table)//
				.x(" %s", () -> {
					if (DELETE.this.whereCodition == null) { return StringFormatter.Type.Ignore; }
					return "WHERE " + this.whereCodition.getSentence();
				})//
				.format();
	}

	@Override
	public List<Object> getParams() {
		if (this.whereCodition == null) { return Collections.emptyList(); }
		return this.whereCodition.getParams();
	}

}
